//
//  AdviceViewController.m
//  DaJiang
//
//  Created by sally on 16/10/5.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "AdviceViewController.h"
#import "CellManager.h"

@interface AdviceViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) UITextView *textView;

@end

@implementation AdviceViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"意见反馈";
  self.footerView.height = 100;
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
  [self.tableData addObject:@[ @"手机号" ]];
  [self.tableData addObject:@[ @"请输入您的意见与建议，我们将不断改进", @"" ]];
}

- (IBAction)actionOnSend:(id)sender {
  
  if (self.textView.text.length > 0) {
    if (self.textField.text.length > 0) {
      if (![OperationManager isMobileNumber:self.textField.text]) {
        [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入正常格式的联系方式"];
        return;
      }
    }
    [[CommonMethod sharedCommonMethod]createProgressHUDInView:self.view withMessage:@""];
    
    [UserModel submitFeedbackWithPhone:self.textField.text content:self.textView.text completion:^(BOOL isSucceed){
      if (isSucceed) {
        [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"已提交反馈，请等候处理！"];
        //清理编辑框里面数据
        [self.tableView reloadData];
      } else {
        NSError *error = [UserModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
    
  }else{
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入反馈意见哦！"];
  }

}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[self.tableData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.section == 1 && indexPath.row == 1) { //最后一项
    return GTextViewHeight - NavHeight;
  }
  return NavHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

  //主标题
  [CellManager configCellNormalText:cell];
  cell.textLabel.text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

  //列表项点击性
  cell.selectionStyle = UITableViewCellSelectionStyleNone;

  //编辑行
  if (indexPath.section == 0) {
    self.textField = [CellManager configCellNormalTextField:cell];
    [cell.contentView addSubview:self.textField];
  }

  //编辑框
  if (indexPath.section == 1 && indexPath.row == 1) { //最后一项
    self.textView = [CellManager configCellTextView:cell];
    [cell.contentView addSubview:self.textView];
  }

  return cell;
}

@end
